package com.frxuexi.document.view

import android.os.Environment
import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.document.DocIntent
import com.frxuexi.document.DocViewModel
import com.frxuexi.lib_base.BaseApp
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.HttpType
import com.frxuexi.lib_base.R
import com.frxuexi.lib_base.UiState
import com.frxuexi.lib_base.entity.LocalEntity
import kotlinx.coroutines.launch

@Composable
fun LocalPage(navHostController: NavHostController,vm:DocViewModel= hiltViewModel()) {

    val file=BaseApp.context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)!!
    var list = remember {
        SnapshotStateList<LocalEntity>()
    }

    var checkedName by remember {
        mutableStateOf("")
    }

    var checkedPath by remember {
        mutableStateOf("")
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        if(it.type==HttpType.SCAN){
                            Log.i("===",it.data.toString())
                            list.addAll(it.data as Collection<LocalEntity>)
                        }else{
                            ToastUtils.showLong("上传成功")
                            navHostController.popBackStack()
                        }
                    }

                    else -> {}
                }
            }
        }

        vm.sendIntent(DocIntent.scanFile(file))
    })

    Column {
        Box(contentAlignment = Alignment.TopEnd,) {
            BaseTitle(title = "本地文件选择", navHostController = navHostController)
            Text(text = "上传", color = Color.White,
                modifier = Modifier
                    .padding(20.dp)
                    .background(colorResource(id = R.color.orange), RoundedCornerShape(8.dp))
                    .padding(10.dp, 2.dp).clickable {
                        vm.sendIntent(DocIntent.upload(checkedName,checkedPath))
                    })
        }
        LazyColumn(content = {
            items(list){
                LocalItem(entity = it,it.fname==checkedName){
                    checkedName=it.fname
                    checkedPath=it.path
                }
            }
        })
    }
}

@Composable
fun LocalItem(entity: LocalEntity,checked:Boolean,onClick: () -> Unit){
    /*var checked by remember {
        mutableStateOf(false)
    }*/
    Row(Modifier.padding(20.dp,8.dp)) {
        Box(contentAlignment = Alignment.TopEnd) {
            Image(painter = painterResource(id = R.drawable.icon3), contentDescription = "",
                Modifier.size(50.dp))
            var img=if(checked)com.frxuexi.document.R.drawable.ic_action_check else com.frxuexi.document.R.drawable.ic_action_uncheck
            Image(painter = painterResource(id = img), contentDescription = "",
                Modifier
                    .size(16.dp)
                    .offset(5.dp, -8.dp)
                    .clickable {
                        onClick()
//                        checked=!checked
                    })
        }
        Spacer(modifier = Modifier.width(10.dp))
        Column(Modifier.height(50.dp), verticalArrangement = Arrangement.SpaceAround) {
            Text(text = entity.fname)
            Text(text = entity.path, maxLines = 1, overflow = TextOverflow.Ellipsis, fontSize = 14.sp, color = Color.Gray)
        }
    }
}